// Token相关操作
const TOKEN_KEY = 'admin_token'
const USER_KEY = 'admin_user'

export const getToken = (): string | null => {
  return localStorage.getItem(TOKEN_KEY)
}

export const setToken = (token: string): void => {
  localStorage.setItem(TOKEN_KEY, token)
}

export const removeToken = (): void => {
  localStorage.removeItem(TOKEN_KEY)
}

export const hasToken = (): boolean => {
  return !!getToken()
}

// 用户信息相关操作
export const getUserInfo = () => {
  const userStr = localStorage.getItem(USER_KEY)
  return userStr ? JSON.parse(userStr) : null
}

export const setUserInfo = (user: any): void => {
  localStorage.setItem(USER_KEY, JSON.stringify(user))
}

export const removeUserInfo = (): void => {
  localStorage.removeItem(USER_KEY)
}

// 清除所有认证信息
export const clearAuth = (): void => {
  removeToken()
  removeUserInfo()
}

// 检查是否已登录
export const isAuthenticated = (): boolean => {
  return hasToken() && !!getUserInfo()
}